<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//课程
class Course extends MY_Controller {

	function __construct(){
		parent::__construct();
		
	}	
	
	//start
	/**
	* @name 获取我的课程
	* @param page false int 页面
	* @param size false int 数量
	* @return 
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		$page = $this->input->post('page',null,1);
		$size = $this->input->post('size',null,10);
		
		
		$this->load->model('Sport/CourseModel','Course');
		
		$arr = array(
			'cid'=>$user_id
		);
		$res = $this->Course->get_list($arr,$page,$size);
		
		$this->data_out($res);
	}
	
	//start
	/**
	* @name 获取我的全部课程
	* @return 
	*/
	//end
	function get_lists(){
		$user_id = $this->user_id;		
		$this->load->model('Sport/CourseModel','Course');		
		$arr = array(
			'cid'=>$user_id
		);
		$res = $this->Course->get_lists($arr);		
		$this->data_out($res);
	}
	
	//start
	/**
	* @name 添加课程
	* @param type_id false int 课程类型0私教1团教
	* @param name ture char 名称
	* @param title ture char 标题
	* @param theme_img ture char 课程封面图
	* @param nandu false char 课程难度
	* @param ranzhi false char 目标燃脂量(千卡)
	* @param duration false int 运动时长(分)
	* @param max_num ture int 允许最大报名总人数
	* @param suitable false char 适用人群，逗号分隔
	* @param info ture char 简介
	* @param content ture char 详细介绍
	* @param sports ture array 动作ID
	* @param shop_id ture char 场馆ID
	* @param status false int  状态0.不显示，1.显示
	* @return 
	*/
	//end
	function add(){
		$user_id = $this->user_id;
		
		$sports = $this->input->post('sports',null,'');
		if(empty($sports)){
			$this->err_out('数据错误');
		}
		
		$sports = explode(',',$sports);
		if(count($sports)<=0){
			$this->err_out('请选择动作');
		}
		

		$time = time();

		
		
		$arr = array(
			'type_id'=>$this->input->post('type_id',null,'0'),
			'name'=>$this->input->post('name',null,''),
			'title'=>$this->input->post('title',null,''),
			'theme_img'=>$this->input->post('theme_img',null,''),
			'nandu'=>$this->input->post('nandu',null,''),
			'ranzhi'=>$this->input->post('ranzhi',null,''),
			'duration'=>$this->input->post('duration',null,''),
			'max_num'=>$this->input->post('max_num',null,10),
			'suitable'=>$this->input->post('suitable',null,''),
			'info'=>$this->input->post('info',null,''),
			'content'=>$this->input->post('content',null,''),
			'status'=>$this->input->post('status',null,1),
			'cid'=>$user_id,
			'shop_id'=>$this->input->post('shop_id',null,1),
			'addtime'=>$time
		);
		
		$ver_arr = array(
			'name'=>"课程名称不能为空",
			'ranzhi'=>"燃脂量不能为空",
			'shop_id'=>"请选择场馆",
		);
		
		$result = $this->CheckData($ver_arr,$arr);
		
		if($result){
			$this->err_out($result);
		}
		
		$this->load->model('Sport/SportModel','Sport');
		$ls_list = $this->Sport->ids_get_data($sports);
		$duration = 0;
		$ids = array();
		foreach($ls_list as $val){
			$duration += $val['duration'];
			$ids[] = $val['id'];
		}
		$arr['sport'] = implode(',',$ids);
		$arr['duration'] = $duration;
		
		if(is_array($arr['suitable'])){
			$suitable = $arr['suitable'];
		}
		else{
			$suitable = explode(',',$arr['suitable']);
		}
		$arr['suitable'] = json_encode($suitable);
		
		
		
		$this->load->model('Sport/CourseModel','Course');
		$this->Course->add($arr);

		
		$this->data_out(1);
	}
	//start
	/**
	* @name 获取场地	
	* @return 
	*/
	//end
	function get_shop(){
		$user_id = $this->user_id;		
		$this->load->model('Index/ShopModel','shop');
		
		$arr = array(
			
		);
		$res = $this->shop->get_list($arr);
		
		$this->data_out($res);
	}
}
